package com.mall.web.util;

import java.text.NumberFormat;

import com.ibm.icu.math.BigDecimal;

public class NumberUtil {
	public static String getPercent(int x, int total, int precision) {
		// 创建一个数值格式化对象
		NumberFormat numberFormat = NumberFormat.getInstance();
		// 设置精确到小数点后2位
		numberFormat.setMaximumFractionDigits(precision);
		String result = numberFormat.format((float) x / (float) total * 100);
		return result;
	}

	public static String getPercentByShengyu(int shengyu, int total, int precision) {
		return getPercent(total - shengyu, total, precision);
	}

	public static void main(String[] args) {
		System.out.println(getPercent(100, 400, 2));
		System.out.println(getPercentByShengyu(300, 400, 2));
	}

}
